home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 254 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: ldb.han.de!W.FANDRYCH
  2. From: W.FANDRYCH@LDB.han.de (Wolfgang H. A. Fandrych)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reclaiming memory allocated recursively
  5. Date: 3 Jan 96 12:42:00 +0100
  6. Message-ID: <60BE3DU0vlB@ldb-wf.ldb.HAN.DE>
  7. References: <4c8f8q$ln9@yarrow.wt.com.au>
  8. X-Mailer: CrossPoint v3.02
  9. X-Gateway: ZCONNECT UH ldb.han.de [UUCPfZ V5.69 U002]
  10.  
  11. Hi to all and hi to bvarley@yarrow.wt.com.au!
  12.  
  13. On 02.01.96, you wrote about
  14. "Reclaiming memory allocated recursively":
  15.  
  16. >     struct node *freetree (struct node *p)
  17. >             {
  18. >             if (p != NULL)
  19. >                 {
  20. >                 freetree (p -> left)   ;
  21. >                 free (p -> data_pointer) ;
  22. >                 freetree (p -> right)  ;
  23. >                 }
  24. >             return (0)  ;
  25. >             }
  26. >
  27. > seems unlikely to work, because the memory required to navigate
  28. > through the tree is being deallocated as the process proceeds. In
  29. > fact, the system crashes when I run the routine.
  30. Why don't you simply change the order of your commands?
  31. If you call freetree(p -> right) just before you call
  32. free(p -> data_pointer) you'll free the subtree before deallocating the  
  33. pointer.
  34.  
  35.  
  36. Bye
  37.                 Alwin.
  38. --
  39. If you've got an idea         Wolfgang Hermann Alwin           PGP-Key
  40. for a nice signature                  Fandrych              available on
  41. send email to:                 W.Fandrych@LDB.HAN.DE           request
  42.  
  43.